package com.yalemang.photovalut.base.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.yalemang.framework.log.LogX
import com.yalemang.photovalut.base.databinding.DialogBottomFragmentBinding
import com.yalemang.photovalut.base.helper.CommonDrawableHelper
import com.yalemang.photovalut.res.R
import java.lang.ref.WeakReference

class CommonBottomDialog : BottomSheetDialogFragment() {

    private var title: String = "title"
    private var color: Int = 0
    private var tabs = ArrayList<Tab>()
    private lateinit var mBinding: DialogBottomFragmentBinding
    private lateinit var mTabClickListener: (Tab)->Unit

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = DialogBottomFragmentBinding.inflate(inflater)
        initView()
        return mBinding.root
    }

    @SuppressLint("ResourceAsColor")
    private fun initView() {
        mBinding.tvTitleBottom.setTextColor(Color.WHITE)
        mBinding.tvTitleBottom.background = CommonDrawableHelper.Builder().setTop(8f).setColor(color).builder()
        mBinding.rvBottom.layoutManager = LinearLayoutManager(requireActivity())
        val bottomDialogAdapter = BottomDialogAdapter(tabs)
        bottomDialogAdapter.itemClickListener = object : BottomDialogAdapter.ItemClickListener{
            override fun click(tab: Tab) {
                if (this@CommonBottomDialog::mTabClickListener.isInitialized){
                    mTabClickListener.invoke(tab)
                }
                dismiss()
            }
        }
        mBinding.rvBottom.adapter = bottomDialogAdapter
    }


    fun show(fragmentActivity: FragmentActivity) {
        show(fragmentActivity.supportFragmentManager, "")
    }

    class Builder {
        private var title: String = "title"
        private var color: Int = 0
        private var tabs = ArrayList<Tab>()
        private lateinit var wrfActivity: WeakReference<FragmentActivity>
        private lateinit var mTabClickListener: (Tab)->Unit

        fun attachContext(context: Context): Builder {
            if (context is FragmentActivity) {
                wrfActivity = WeakReference(context)
                LogX.d("context获取成功")
            } else {
                LogX.e("context获取错误，非FragmentActivity")
            }
            return this
        }

        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setColor(color: Int): Builder {
            this.color = color
            return this
        }

        fun addTab(tab: Tab): Builder {
            tabs.add(tab)
            return this
        }

        fun addTab(string: String): Builder {
            tabs.add(Tab(string))
            return this
        }

        fun setTabData(tabs: ArrayList<Tab>): Builder {
            this.tabs = tabs
            return this
        }

        fun setTabClickListener(tabClickListener: (Tab)->Unit) : Builder{
            this.mTabClickListener = tabClickListener
            return this
        }

        fun builder(): CommonBottomDialog {
            val commonBottomDialog = CommonBottomDialog()
            commonBottomDialog.title = this.title
            commonBottomDialog.color = this.color
            commonBottomDialog.tabs = this.tabs
            commonBottomDialog.mTabClickListener = this.mTabClickListener
            return commonBottomDialog
        }
    }

    class Tab(var content: String = "")
}